﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class PipeSize : System.Windows.Forms.Form
	{
		public PipeSize()
		{
			this.InitializeComponent();
			this.btnDeleteItems.Image = Resource.GetImage("delete16");
		}

		private void PipeSize_Load(object sender, EventArgs e)
		{
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAddItems, "添加");
			toolTip.SetToolTip(this.btnReName, "编辑");
			toolTip.SetToolTip(this.btnDeleteItems, "删除");
			if (ElectricSystemSettingData.ConduitTypeDatas == null)
			{
				return;
			}
			this.cmbPipeType.DisplayMember = "ConduitTypeSet";
			foreach (ConduitTypeSetClass item in ElectricSystemSettingData.ConduitTypeDatas)
			{
				this.cmbPipeType.Items.Add(item);
			}
			if (this.cmbPipeType.Items.Count > 0)
			{
				this.cmbPipeType.SelectedIndex = 0;
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			new CreateItem(this.cmbPipeType, LineType.ConduitType).ShowDialog();
			PipeSize.pipeSize = true;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (this.cmbPipeType.Items.Count == 1)
			{
				MessageBox.Show("至少有一种线管类型！删除失败", "盈建科提示");
				return;
			}
			if (this.cmbPipeType.SelectedItem != null && DialogResult.Yes == MessageBox.Show("是否要将选中项删除？", "盈建科提示", MessageBoxButtons.YesNo))
			{
				ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbPipeType.SelectedItem;
				foreach (ConduitTypeSetClass conduitTypeSetClass2 in ElectricSystemSettingData.ConduitTypeDatas)
				{
					if (conduitTypeSetClass2.ConduitTypeSet == conduitTypeSetClass.ConduitTypeSet)
					{
						ElectricSystemSettingData.ConduitTypeDatas.Remove(conduitTypeSetClass2);
						break;
					}
				}
				foreach (WirePipeSizeData wirePipeSizeData in ElectricSystemSettingData.WirePipeSizeDatas)
				{
					if (wirePipeSizeData.WirePipeType == conduitTypeSetClass.Id)
					{
						ElectricSystemSettingData.WirePipeSizeDatas.Remove(wirePipeSizeData);
						break;
					}
				}
				this.cmbPipeType.Items.RemoveAt(this.cmbPipeType.SelectedIndex);
				if (this.cmbPipeType.Items.Count > 0)
				{
					this.cmbPipeType.SelectedIndex = 0;
				}
				PipeSize.pipeSize = true;
			}
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Add();
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
		}

		private void cmbPipeType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Clear();
			ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbPipeType.SelectedItem;
			if (conduitTypeSetClass == null)
			{
				return;
			}
			int num = 0;
			foreach (WirePipeSizeData wirePipeSizeData in ElectricSystemSettingData.WirePipeSizeDatas)
			{
				if (wirePipeSizeData.WirePipeType == conduitTypeSetClass.Id)
				{
					this.dataGridView1.Rows.Clear();
					List<double> sizeItems = wirePipeSizeData.SizeItems;
					if (sizeItems.Count == 0)
					{
						return;
					}
					this.dataGridView1.Rows.Add(sizeItems.Count);
					int num2 = 0;
					using (List<double>.Enumerator enumerator2 = sizeItems.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							double value = enumerator2.Current;
							this.dataGridView1.Rows[num].ReadOnly = true;
							this.dataGridView1.Rows[num2].Cells[0].Value = Convert.ToInt32(value);
							num2++;
							num++;
						}
						break;
					}
				}
			}
			this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		private void cmbPipeType_MouseClick(object sender, MouseEventArgs e)
		{
		}

		public void SaveEnvTempData()
		{
			bool flag = true;
			PipeSize.pipeSize = true;
			ConduitTypeSetClass conduitTypeSetClass = (ConduitTypeSetClass)this.cmbPipeType.SelectedItem;
			ElectricSystemSettingData.ConduitTypeDatas.Clear();
			if (conduitTypeSetClass == null)
			{
				return;
			}
			foreach (object obj in this.cmbPipeType.Items)
			{
				ConduitTypeSetClass item = (ConduitTypeSetClass)obj;
				ElectricSystemSettingData.ConduitTypeDatas.Add(item);
			}
			foreach (WirePipeSizeData wirePipeSizeData in ElectricSystemSettingData.WirePipeSizeDatas)
			{
				if (wirePipeSizeData.WirePipeType == conduitTypeSetClass.Id)
				{
					flag = false;
					List<double> list = new List<double>();
					for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
					{
						if (this.dataGridView1.Rows[i].Cells[0].Value != null && this.dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
						{
							list.Add(Convert.ToDouble(this.dataGridView1.Rows[i].Cells[0].Value));
						}
					}
					wirePipeSizeData.SizeItems = list;
					break;
				}
			}
			if (flag)
			{
				WirePipeSizeData wirePipeSizeData2 = new WirePipeSizeData();
				wirePipeSizeData2.WirePipeType = conduitTypeSetClass.Id;
				List<double> list2 = new List<double>();
				for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
				{
					if (this.dataGridView1.Rows[j].Cells[0].Value != null && this.dataGridView1.Rows[j].Cells[0].Value.ToString() != "")
					{
						list2.Add(Convert.ToDouble(this.dataGridView1.Rows[j].Cells[0].Value));
					}
				}
				wirePipeSizeData2.SizeItems = list2;
				ElectricSystemSettingData.WirePipeSizeDatas.Add(wirePipeSizeData2);
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.SaveEnvTempData();
		}

		private void btnAddItems_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DataGridViewAddRow(1, this.dataGridView1))
			{
				this.SaveEnvTempData();
			}
		}

		private void btnDeleteItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count >= 1 && this.dataGridView1.SelectedRows.Count > 0)
			{
				this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
				this.SaveEnvTempData();
				PipeSize.pipeSize = true;
			}
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex;
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			this.dgvTxt = (DataGridViewTextBoxEditingControl)e.Control;
			this.dgvTxt.SelectAll();
			this.dgvTxt.KeyPress += this.Cells_KeyPress;
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			ElectricSystemSettingData.keyPressXS(e, this.dgvTxt);
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DatagridviewEditRow(1, this.dataGridView1))
			{
				this.SaveEnvTempData();
			}
		}

		public static bool pipeSize;

		public DataGridViewTextBoxEditingControl dgvTxt;
	}
}
